package chapter4;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;

public class Ex3 extends JApplet {
  public static void main(String s[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JApplet applet = new Ex3();
    applet.init();
    frame.getContentPane().add(applet);
    frame.pack();
    frame.setVisible(true);
  }
  
  public void init() {
    JPanel panel = new JPanel() {
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Shape t = new Triangle(new Point(50,80),new Point(450,200), new Point(150, 400));
        ((Graphics2D)g).fill(t);
      }
    };
    panel.setBackground(Color.white);
    panel.setPreferredSize(new Dimension(500,500));
    getContentPane().add(panel);
  }
}

class Triangle implements Shape {
  GeneralPath path;

  public Triangle(Point a, Point b, Point c) {
    path = new GeneralPath();
    path.moveTo(a.x, a.y);
    path.lineTo(b.x, b.y);
    path.lineTo(c.x, c.y);
    path.closePath();
  }

  public boolean contains(Rectangle2D rect) {
    return path.contains(rect);
  }

  public boolean contains(Point2D point) {
    return path.contains(point);
  }

  public boolean contains(double x, double y) {
    return path.contains(x, y);
  }

  public boolean contains(double x, double y, double w, double h) {
    return path.contains(x, y, w, h);
  }

  public Rectangle getBounds() {
    return path.getBounds();
  }

  public Rectangle2D getBounds2D() {
    return path.getBounds2D();
  }

  public PathIterator getPathIterator(AffineTransform at) {
    return path.getPathIterator(at);
  }

  public PathIterator getPathIterator(AffineTransform at, double flatness) {
    return path.getPathIterator(at, flatness);
  }

  public boolean intersects(Rectangle2D rect) {
    return path.intersects(rect);
  }

  public boolean intersects(double x, double y, double w, double h) {
    return path.intersects(x, y, w, h);
  }
}
